package com.geek.log4jcontrol.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.geek.log4jcontrol.commons.ConfigConstants;
import com.geek.log4jcontrol.commons.FinalConstants;
import com.geek.util.FileContextUtil;

/**
 * @package com.geek.log4jcontrol.core
 * @description log4j历史配置的操作
 * @author Goliath
 * @createTime 2012-8-3 上午09:54:45
 * @modifyTime
 */
public class ConfigHisControler {

	private ConfigHisControler() {
	}

	private static ConfigHisControler configHisControler = new ConfigHisControler();

	public static ConfigHisControler getInstance() {
		return configHisControler;
	}

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * @package com.geek.log4jcontrol.core
	 * @description 获取log4j的历史配置
	 * @param configCode 配置编码
	 * @return log4j的历史配置
	 * @returnType ConfigHis
	 * @author Goliath
	 * @createTime 2012-8-3 上午10:46:23
	 * @modifyTime
	 */
	public ConfigHis getConfig(String configCode) {
		ConfigHis oConfigHis = null;
		String configFilePath = ConfigConstants.CONFIGHIS_PATH + File.separator + configCode
				+ FinalConstants.SUFFIX_DEFAULT;
		File configHisFile = new File(configFilePath);
		if (configHisFile.exists()) {
			oConfigHis = new ConfigHis();
			String context = FileContextUtil.loadContextString(configHisFile);
			String createTimeStr = configCode.substring(FinalConstants.LC.length(), configCode.length());
			Date createTime = null;
			try {
				createTime = sdf.parse(createTimeStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			oConfigHis.setConfigCode(configCode);
			oConfigHis.setContext(context);
			oConfigHis.setCreateTime(createTime);
			oConfigHis.setFilePath(configFilePath);
		}
		return oConfigHis;
	}

	/**
	 * @package com.geek.log4jcontrol.core
	 * @description 获取log4j的历史配置列表
	 * @return log4j的历史配置列表
	 * @returnType List<ConfigHis>
	 * @author Goliath
	 * @createTime 2012-8-3 上午10:46:49
	 * @modifyTime
	 */
	public List<ConfigHis> getConfigList() {
		List<ConfigHis> configHisList = new ArrayList<ConfigHis>();
		File configFileDir = new File(ConfigConstants.CONFIGHIS_PATH + File.separator);

		File[] configFileList = configFileDir.listFiles(new FilenameFilter() {
			/* (non-Javadoc)
			 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
			 */
			public boolean accept(File dir, String fileName) {
				return (isConfigProp(fileName));
			}

			public boolean isConfigProp(String fileName) {
				fileName = fileName.toLowerCase();
				if (fileName.endsWith(FinalConstants.SUFFIX_DEFAULT)
						&& fileName.startsWith(FinalConstants.LC.toLowerCase())) {
					return true;
				} else {
					return false;
				}
			}
		});

		for (File configFile : configFileList) {

			String configFileName = configFile.getName();
			int endIndex = (int) (configFileName.length() - FinalConstants.SUFFIX_DEFAULT.length());
			configFileName = configFileName.substring(0, endIndex);
			ConfigHis oConfigHis = getConfig(configFileName);
			configHisList.add(oConfigHis);
		}

		return configHisList;
	}

	public void makeConfigHisByPath(String filePath) {
		String configStr = FileContextUtil.loadContextString(filePath);
		if (configStr != null) {
			makeConfigHis(configStr);
		}
	}

	public void makeConfigHis(File file) {
		String configStr = FileContextUtil.loadContextString(file);
		if (configStr != null) {
			makeConfigHis(configStr);
		}
	}

	public void makeConfigHis(String configStr) {
		String configCode = FinalConstants.LC + sdf.format(new Date());
		String fileName = configCode + FinalConstants.SUFFIX_DEFAULT;
		File ConfigHisFile = new File(ConfigConstants.CONFIGHIS_PATH + File.separator + fileName);
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(ConfigHisFile));
			bw.write(configStr);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void makeConfigHisByConfigCode(String configCode) {
		ConfigHis oConfigHis = getConfig(configCode);
		makeConfigHis(oConfigHis.getContext());
	}
}